/**
 * Created by win 10 on 11/22/2017.
 */
import Vue from 'vue'
export default{
  // 取参数
  getQueryStringByName: function (name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
  },
  // 设置cookie
  setCookie: function (cname, cvalue, exdays) {
    var d = new Date()
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
    var expires = 'expires=' + d.toUTCString()
    console.info(cname + '=' + cvalue + '; ' + expires)
    document.cookie = cname + '=' + cvalue + '; ' + expires
    console.info(document.cookie)
  },
  // 获取cookie
  getCookie: function (cname) {
    var name = cname + '='
    var ca = document.cookie.split(';')
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i]
      while (c.charAt(0) === ' ') {
        c = c.substring(1)
      }
      if (c.indexOf(name) !== -1) return c.substring(name.length, c.length)
    }
    return null
  },
  // 清除cookie
  clearCookie: function (cname) {
    this.setCookie(cname, '', -1)
  },
  // 检测cookie
  checkCookie: function (cname) {
    var user = this.getCookie(cname)
    if (user !== '') {
      alert('Welcome again ' + user)
    } else {
      user = prompt('Please enter your name:', '')
      if (user !== '' && user !== null) {
        this.setCookie(cname, user, 365)
      }
    }
  },
  // url
  fineURL: function () {
    return 'http://www.finewest.cn'
  },
  ajax: function (method, url, data) {
    Vue.http({
      method: method,
      url: url,
      params: data
    })
  },
  // 滚动到指定位置 (id 指定位置的ID，duration 控制速度，time 控制时间)
  scrollPageY: function (id, duration, time) {
    time = time || 10
    var timer = null
    clearInterval(timer)
    timer = setInterval(function () {
      //  滚动条位置 top
      var bodyScrollTop = document.documentElement.scrollTop || document.body.scrollTop
      var allHeight = document.body.scrollHeight || document.documentElement.scrollHeight
      var clienHeight = document.body.clientHeight > document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight
      //  div 距离body顶部高度
      var divScrollTop = document.querySelector(id).offsetTop
      var nowTop = divScrollTop - bodyScrollTop
      duration = duration || 8
      var speed = nowTop / duration
      speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed)
      if (Math.abs(speed) <= 1 || (parseInt(speed) > 0 && parseInt(bodyScrollTop + clienHeight) >= parseInt(allHeight))) {
        clearInterval(timer)
      } else {
        document.documentElement.scrollTop = speed > 0 ? Math.ceil(bodyScrollTop + speed) : Math.floor(bodyScrollTop + speed)
        document.body.scrollTop = speed > 0 ? Math.ceil(bodyScrollTop + speed) : Math.floor(bodyScrollTop + speed)
      }
    }, time)
  },
  // 禁止touch
  prevent: function (event) {
    event = window.event || event
    event.preventDefault()
  },
  // 判断有无class值
  hasClass: function (claName) {
    return !!this.className.match(new RegExp( '(\\s|^)' + claName + '(\\s|$)'))
  },
  // 添加class
  addClass: function (claName) {
    if (!this.hasClass(claName)) this.className += ' ' + claName
  },
  // 删除class
  removeClass: function (claName) {
    if (this.hasClass(claName)) {
      var reg = new RegExp('(\\s|^)' + claName + '(\\s|$)')
      this.className = this.className.replace(reg, ' ')
    }
  },
  // 获取相邻元素
  siblings: function (ele) {
    var nodes = []
    var _ele = ele
    while (ele === ele.previousSibling) {   // 相当于一个for循环，直到ele不等于ele.previousSibling结束
      if (ele.nodeType === 1) {
        nodes.push(ele)
      }
    }
    var ele = _ele
    while (ele === ele.nextSibling) {
      if (ele.nodeType === 1) {
        nodes.push(ele)
      }
    }
    return nodes
  },
  // 格式化时间格式
  parseCastingDate: function (d, f) {
    if (d && d !== '') {
      var date = new Date(d)
      return this.format(date, f)
    } else {
      return false
    }
  },
  format: function (d, fmt) { // author: meizz
    if (fmt === 'null') {
      return '未知'
    }
    var o = {
      'M+': d.getMonth() + 1,  // 月份
      'd+': d.getDate(), // 日
      'h+': d.getHours(), // 小时
      'm+': d.getMinutes(), // 分
      's+': d.getSeconds(), // 秒
      'q+': Math.floor((d.getMonth() + 3) / 3), // 季度
      'S': d.getMilliseconds() // 毫秒

    }
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
      }
    }
    return fmt
  }
}
